# -*- coding: utf-8 -*-

# -------------------------------
# Author    : linlingwei
# Email     : 951035650@qq.com
# File Name : ikCore.py
# Date      : 2019/12/10 
# IDE       : PyCharm
# Version   : 1.0.1
# -------------------------------

import maya.cmds as mc
import maya.mel as mel

import configs.constants.attribute_constant as ac
import configs.constants.nodeType_constant as nc
import configs.constants.suffix_constant as sc
import configs.constants.body_constant as bc
import configs.constants.global_constant as gc
reload(ac)
import scripts.commonCmds as con
import scripts.curveCmds as crc

class IkCore(object):
    def __init__(self,ikType = "ikSCsolver",lname="test"):
        self.ikType = ikType
        self.lname = lname
        self._ikhandle = None
        self.jotlist = None
        self.ikhandle_loc = None
        self.jotRoot = None
        self.ikCurve = None
        
        self.localGrp = con.lname(lname,sc.sGrp)
        self.localBridge = con.lname(lname,sc.sBridge)
        
    
    def _init(self):
        
        if not mc.objExists(self.localGrp):
            grp = mc.createNode(nc.tTransform,name = self.localGrp)
        if not mc.objExists(self.localBridge):
            bridge = mc.createNode(nc.tTransform,name = self.localBridge,parent=self.localGrp)
            con.lockHideAttr(bridge)
        return
    
    def ikhandle(self,sjot,ejot,createCurve=False):
    
        if self.ikType != 'ikSplineSolver':
            ikh = mc.ikHandle( sj=sjot, ee=ejot,sol=self.ikType,
                               name = con.lname(self.lname,sc.sIkhandle))
        else:
            if createCurve != True and createCurve:
                ikh = mc.ikHandle(sj=sjot, ee=ejot, sol=self.ikType,
                                  name=con.lname(self.lname, sc.sIkhandle), ccv=False, c=createCurve)

                self.ikCurve = createCurve
            else:
                ikh = mc.ikHandle(sj=sjot, ee=ejot, sol=self.ikType,
                                  name=con.lname(self.lname, sc.sIkhandle), ccv=True, ns=3)
                self.ikCurve = mc.rename(mc.ikHandle(ikh[0],q=True,c=True), con.lname(self.lname, sc.sCrv))
        mc.rename(ikh[1],con.lname(self.lname,sc.sEffector))
        self._ikhandle = ikh[0]
        mc.hide(self._ikhandle )
        self.ikhandle_loc = con.objToLocator(self._ikhandle,name=con.lname(self._ikhandle),
                                             parent=self.localGrp)
        mc.parent(self._ikhandle,self.ikhandle_loc)
        # mc.parent(sjot,self.localGrp)
        self.get_jointList()
        
        # if self.ikType == 'ikSplineSolver':
        #     self.ikCurve = mc.rename(ikh[2],con.lname(self.lname,sc.sCrv))
        #     mc.parent(self.ikCurve,self.localGrp)
        return ikh[0]
    
    def jot_root(self):
        self.jotRoot = con.emptyGroup(self.jotlist[0],[bc.gRoot])
        return
    
    def rename_joint(self):
        
        for i,jot in enumerate(self.get_jointList(),1):
            mc.rename(jot,con.lname(self.lname,i,sc.sJot))
        
        return
    
    def get_jointList(self):
        if not self._ikhandle:
            return
        jointList = mc.ikHandle(self._ikhandle, q=True, jl=True)
        effect = mc.ikHandle(self._ikhandle, q=True, ee=True)
        endJot = mc.listConnections(effect + ".tx", s=True, d=False)[0]
        jointList.append(endJot)
        self.jotlist = jointList
        return jointList

if __name__ == '__main__':
    pass